/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utilidades;

import Sistema.*;

/**
 *
 * @author diogo
 */
public abstract class UtilityHTML {

    public static final String BREAK_LINE = "\n"; // optional variable. if wants to separate the code a little
    public static final String TAB = "\t"; // optional variable. if wants to separate the code a little
    public static String END_HTML = "</body></html>";
    public static String STYLE_CAPITALIZE = "text-transform: capitalize;";

    public static String Basic(String title) {
        return Begin(title) + Body();
    }

    public static String Basic() {
        return Basic("");
    }

    public static String Begin(String title) {
        return "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"
                + "<head>" + BREAK_LINE
                + "<title>" + title + "</title> "
                + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"
                + "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" />"
                //+ "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\" /> "
                //+ "<link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\" />"
                + BREAK_LINE;
    }

    public static String Begin() {
        return Begin("");
    }

    public static String Body(String complement) {
        return "</head><body " + complement + " >" + BREAK_LINE;
    }

    public static String Body() {
        return Body("");
    }

    public static String AddCssStyle(String filePath) {
        return "<link href=\"" + filePath + "\" rel=\"stylesheet\" type=\"text/css\" />" + BREAK_LINE;
    }

    public static String AddJavascript(String filePath) {
        return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>" + BREAK_LINE;
    }

    public static String NewInput(String type, String name, Object value, String adds) {
        return String.format("<input type = \"%s\" name=\"%s\" value = \"%s\" %s />" + BREAK_LINE, type, name, value.toString(), adds);
    }

    public static String NewInput(String type, String name, Object value) {
        return NewInput(type, name, value, "");
    }

    public static String NewInput(String type, String name) {
        return NewInput(type, name, "", "");
    }

    public static String NewComboBox(String name, String[] values, String selected, String[] display, String adds) {
        return NewComboBox(name, values, selected, display, adds, false);
    }

    public static String NewComboBox(String name, String[] values, String selected, String[] display, String adds, boolean onlyOptions) {
        StringBuilder output = new StringBuilder("");
        if (!onlyOptions) {
            //output.append("<select name=\"" + name + "\""+ adds +">" + BREAK_LINE);
            output.append("<select name=\"").append(name).append("\"").append(adds).append(">" + BREAK_LINE);
        }
        if (values.length > 0) {
            for (int x = 0; x < values.length; x++) {
//                System.out.println(String.format("values[%d] = %s", x, values[x]));
                if (selected != null) {
                    //output.append(NewComboItem(values[x], display[x], selected.equals(values[x])));
                    output.append(NewComboItem(values[x], display[x], selected.equals(values[x])));

                } else {
                    output.append(NewComboItem(values[x], display[x], false));

                }
                /*
                output += "<option value=\""+values[x]+"\"";
                if (selected != null) {
                output += String.format(" %s>", (selected.equals(values[x]) ? "selected='true'" : ""));
                } else {
                output += "<option>";
                }
                output += display[x];
                output += "</option>";
                 */
            }
            if (!onlyOptions) {
                output.append( "</select>" + BREAK_LINE);
            }
        }
        return output.toString();
    }

    public static String NewComboBox(String name, String[] options, String selected) {
        return NewComboBox(name, options, selected, options, "");
    }

    public static String NewComboBox(String name, String[] options) {
        return NewComboBox(name, options, null, options, "");
    }

    public static String NewComboItem(String value, String display, boolean selected) {
        String output = "";
        if (display == null) {
            display = value;
        }
        output += String.format(TAB + "<option value=\"%s\" %s> %s </option>", value, (selected ? "selected" : ""), display);
        return output + BREAK_LINE;
    }

    public static String NewHiperlink(String href, String display, String adds) {
        return "<a href=\"" + href + "\" " + adds + " >" + display + "</a>" + BREAK_LINE;
    }

    public static String NewHiperlink(String href, String display) {
        return NewHiperlink(href, display, "");
    }

    public static String NewColumn(Object value, String adds) {
        return String.format("<TD %s> %s </TD>", adds, value);
    }

    public static String NewColumn(Object value) {
        return NewColumn(value, "");
    }

    public static String ExecuteJavascript(String... commands) {
        String output = "<script type=\"text/javascript\">" + BREAK_LINE;
        for (String cmd : commands) {
            output += TAB + cmd + " ;" + BREAK_LINE;
        }
        output += "</script>" + BREAK_LINE;
        return output;
    }

    /**
     * returns a string that redirects a page in a certain time
     *
     * @param href where to go
     * @param time in how mani milliseconds
     * @return a javascript command with
     */
    public static String RedirectPage(String href, int time) {
        return ExecuteJavascript(
                "setTimeout("
                + "function(){"
                + "window.location = \"" + href
                + "\"}, " + time + ")");
    }

    public static String ExecuteContinuousJavascript(String command, int timeInterval) {
        String output = "<script type=\"text/javascript\">" + BREAK_LINE;
        output += TAB + "setInterval(function(){"
                + command + "}, "
                + timeInterval + ")" + BREAK_LINE;
        output += "</script>" + BREAK_LINE;
        return output;
    }

    public static String Capitalize(String text) {
        return String.format("<div style=\"" + STYLE_CAPITALIZE + "\"> %s </div>", text);
    }

    /**
     * Ref: http://www.rgagnon.com/javadetails/java-0306.html
     * @author :  S. Bayer.
     * 
     * @param string
     * @return
     */
    public static String stringToHTMLString(String string) {
        StringBuffer sb = new StringBuffer(string.length());
        // true if last char was blank
        boolean lastWasBlankChar = false;
        int len = string.length();
        char c;

        for (int i = 0; i < len; i++) {
            c = string.charAt(i);
            if (c == ' ') {
                // blank gets extra work,
                // this solves the problem you get if you replace all
                // blanks with &nbsp;, if you do that you loss
                // word breaking
                if (lastWasBlankChar) {
                    lastWasBlankChar = false;
                    sb.append("&nbsp;");
                } else {
                    lastWasBlankChar = true;
                    sb.append(' ');
                }
            } else {
                lastWasBlankChar = false;
                //
                // HTML Special Chars
                if (c == '"') {
                    sb.append("&quot;");
                } else if (c == '&') {
                    sb.append("&amp;");
                } else if (c == '<') {
                    sb.append("&lt;");
                } else if (c == '>') {
                    sb.append("&gt;");
                } else if (c == '\n') // Handle Newline
                {
                    sb.append("&lt;br/&gt;");
                } else {
                    int ci = 0xffff & c;
                    if (ci < 160) // nothing special only 7 Bit
                    {
                        sb.append(c);
                    } else {
                        // Not 7 Bit use the unicode system
                        sb.append("&#");
                        sb.append(new Integer(ci).toString());
                        sb.append(';');
                    }
                }
            }
        }
        return sb.toString();
    }
}
